const { defineConfig } = require("@vue/cli-service");
let { list } = require("./src/mock");
const bodyParser = require('body-parser')
module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    setupMiddlewares(middle, { app }) {
      app.use(bodyParser.json())
      app.get("/api/user/list", (req, res) => {
        const { pageCode = 1, pageSize = 10, value = "" } = req.query;

        const newList = list.filter((v) => {
          if (value.trim() === "") return true;
          return (
            v.name.includes(value) ||
            v.age.toString().includes(value) ||
            v.sex.includes(value) ||
            v.id.toString().includes(value) ||
            v.tel.toString().includes(value)
          );
        });

        res.send({
          code: 200,
          data: newList.slice((pageCode - 1) * pageSize, pageCode * pageSize),
          total: newList.length,
        });
      });

      app.post('/api/user/list/delete', (req, res) => {
        const { id } = req.body;
        list = list.filter(v => v.id !== id);
        res.send({
          code: 200,
          message: '删除成功！！'
        })
      })

      app.post('/api/user/lists/delete', (req, res) => {
        const { ids } = req.body;
        list = list.filter(v => !ids.includes(v.id));

        res.send({
          code: 200,
          message: '删除成功！！'
        })
      })

      return middle;
    },
  },
});
